"""
# -*- coding: utf-8 -*-
# @Time : 2023/4/14 5:33
# @Author : LTY
# @File : L231_PP_Beginning_Page_test.py
# @function : python3.9.2
"""

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QRegExpValidator, QKeyEvent, QIcon
from PyQt5.QtCore import QRegExp            # QT里面正则表达式的类
from PyQt5.QtCore import Qt

from ui_QT.ui_BeginningPage import Ui_MainWindow as Begin_ui_Main
from L231_PP_RSB4BQTtest_LTY_230414 import L231_PP_Main_Page_test as Func


class QT_Begin(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)  # 调用父类构造函数，创建窗体
        self.ui_Begin = Begin_ui_Main()  # 创建UI对象
        self.ui_Begin.setupUi(self)  # 构造UI界面
        self.initUI()

##  ==============自定义功能函数========================
    def initUI(self):
        self.setWindowTitle('登录页面')

        # 用户名设置
        usernameLineEdit = self.ui_Begin.lineEdit_username

        reg = QRegExp('[a-zA-Z0-9]+$')
        username_Validator = QRegExpValidator(self)
        username_Validator.setRegExp(reg)
        usernameLineEdit.setValidator(username_Validator)

        # 密码设置
        passwordLineEdit = self.ui_Begin.lineEdit_password

        reg = QRegExp('[a-zA-Z0-9]+$')
        password_Validator = QRegExpValidator(self)
        password_Validator.setRegExp(reg)
        passwordLineEdit.setValidator(password_Validator)

        passwordLineEdit.setEchoMode(QLineEdit.Password)

        # 退出按钮设置
        self.button1 = self.ui_Begin.pushButton_quit
        self.button1.clicked.connect(self.QUIT)
        self.button1.setShortcut("Esc")

        # 校验按钮设置
        self.button2 = self.ui_Begin.pushButton_log_confirm
        self.button2.clicked.connect(self.CHECK)

        # Enter键联动
        # usernameLineEdit.returnPressed.connect(passwordLineEdit.insert)
        passwordLineEdit.returnPressed.connect(self.button2.click)

    # # Enter按键输入
    # def keyPressEvent(self, QKeyEvent):
    #     if QKeyEvent.key() == Qt.Key_Return:
    #         self.CHECK()

    # 退出函数
    def QUIT(self):
        text = self.sender().text()
        if text == '退出':
            reply = QMessageBox.question(self, 'Quit', '您是否要退出', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
            if reply == QMessageBox.Yes:
                self.close()

    # 校验函数
    def CHECK(self):
        if 'admin' == self.ui_Begin.lineEdit_username.text():
            if '123456' == self.ui_Begin.lineEdit_password.text():
                self.ui_Func = Func.QT_Function()
                self.close()
                self.ui_Func.show()
            else:
                dialog = QDialog()
                dialog.resize(230, 80)
                label1 = QLabel(dialog)
                label1.setText("密码错误，请重新输入密码")
                label1.move(20, 10)
                button = QPushButton('确定', dialog)
                button.clicked.connect(dialog.close)
                button.clicked.connect(self.CLEAR_PASSWORD)
                button.move(70, 40)
                dialog.setWindowTitle('密码错误')
                dialog.setWindowModality(Qt.ApplicationModal)  # 防止子窗口响应后，主串口继续相应
                dialog.exec()

        else:
            dialog = QDialog()
            dialog.resize(230, 80)
            label1 = QLabel(dialog)
            label1.setText("未注册该用户")
            label1.move(70, 10)
            button = QPushButton('确定', dialog)
            button.clicked.connect(dialog.close)
            button.clicked.connect(self.CLEAR_USERNAME)
            button.clicked.connect(self.CLEAR_PASSWORD)
            button.move(70, 40)
            dialog.setWindowTitle('用户名无效')
            dialog.setWindowModality(Qt.ApplicationModal)  # 防止子窗口响应后，主串口继续相应
            dialog.exec()

    def CLEAR_USERNAME(self):
        self.ui_Begin.lineEdit_username.clear()

    def CLEAR_PASSWORD(self):
        self.ui_Begin.lineEdit_password.clear()


##  ============窗体测试程序 ================================
if __name__ == "__main__":  # 用于当前窗体测试
    app = QApplication(sys.argv)  # 创建GUI应用程序
    app.setWindowIcon(QIcon('..\\image\\school(256).ico'))
    form = QT_Begin()  # 创建窗体
    form.show()

    sys.exit(app.exec_())